package com.snail.billing.util;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import com.android.os.MyEngine;

public class BillingUtil {

	public static boolean isNetworkAvailable() {
		ConnectivityManager connManager = (ConnectivityManager) MyEngine.getEngine().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
		if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {
			return true;
		}
		return false;
	}

	public static String getLocalIp() {
		Enumeration<NetworkInterface> netInterfaces = null;
		try {
			StringBuffer result = new StringBuffer();
			netInterfaces = NetworkInterface.getNetworkInterfaces();
			while (netInterfaces.hasMoreElements()) {
				NetworkInterface ni = netInterfaces.nextElement();
				if (ni.isLoopback() || ni.isVirtual() || !ni.isUp())
					continue;
				Enumeration<InetAddress> ips = ni.getInetAddresses();
				while (ips.hasMoreElements()) {
					InetAddress ia = ips.nextElement();
					if (ia instanceof Inet6Address)
						continue;
					result.append(ia.getHostAddress()).append(",");
				}
			}
			return result.toString().substring(0, result.length() - 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 验证帐号密码
	 * 
	 * @param pwd
	 * @return
	 */
	public static boolean validatePwd(String pwd) {
		if (TextUtils.isEmpty((pwd)))
			return false;
		// 判断是否为升序或降序排列
		if (isAscOrDesc(pwd, 1) || isAscOrDesc(pwd, -1)) {
			return false;
		}
		Pattern validPattern = Pattern.compile("^[0-9a-zA-Z]{6,20}$");
		Pattern badPattern = Pattern.compile("^([\\w])\\1+$");
		Matcher validMatcher = validPattern.matcher(pwd);
		Matcher bad = badPattern.matcher(pwd);
		if (bad.matches()) {
			return false;
		} else if (!validMatcher.matches()) {
			return false;
		}
		return true;
	}

	/**
	 * 判断是否为升序或降序
	 * 
	 * @param str
	 * @param flag
	 *            1 -- 升序 -1 -- 降序
	 * @return
	 */
	private static boolean isAscOrDesc(String str, int flag) {
		char[] chars = str.toCharArray();
		for (int i = 1; i < chars.length; i++) {
			char pre = chars[i - 1];
			char cur = chars[i];
			if ((char) (pre + flag) != cur) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 验证手机号
	 * 
	 * @param mobile
	 * @return
	 * @since 2014-12-3
	 */
	public static boolean isValidateMobile(String mobile) {
		if (TextUtils.isEmpty((mobile))) {
			return false;
		}
		Pattern p = Pattern.compile("^[1]([3,8][\\d]|[5][0-3,5-9]|[7][0,6-8])[\\d]{8}$");
		Matcher m = p.matcher(mobile);
		return m.matches();
	}

	/**
	 * 验证email
	 * 
	 * @param email
	 * @return
	 * @since 2014-12-3
	 */
	public static boolean isValidateEmail(String email) {
		if (TextUtils.isEmpty((email))) {
			return false;
		}
		Pattern p = Pattern.compile("^[\\w.-]+@[\\w.-]+(\\.[A-Za-z0-9]{2,})+$");
		Matcher m = p.matcher(email);
		return m.matches();
	}

}
